<?php
/**
 * Created by PhpStorm.
 * User: hanbin
 * Date: 17/4/17
 * Time: 下午9:21
 */

namespace App\Logic;


use App\Server\MemberModel;
use App\Server\ResumeModel;

/**
 * 简历
 * Class ResumeLogic
 * @package App\Logic
 */
class ResumeLogic extends BaseLogic
{

    /**
     * 简历详情
     *
     * @param int $userId
     * @param int $type
     * @return array
     */
    public static function detail($userId = 0, $type = 1)
    {
        if ($userId) {
            $memberInfo = MemberModel::first(['id' => $userId]);
            $detail = ResumeModel::first(['member_id' => $userId, 'type' => $type, 'is_delete' => 0]);
            if ($detail) {
                $result = [];
                $result['id'] = $detail['id'];
                $result['face'] = $memberInfo['face'];
                $result['username'] = $memberInfo['username'];
                $result['school'] = $memberInfo['school'];
                $result['email'] = $memberInfo['email'];
                $result['sex'] = $memberInfo['sex'] == 1 ? "男" : "女";
                $result['mobile'] = $memberInfo['mobile'];
                $result['school_time'] = $memberInfo['school_time'];
                $result['school_major'] = $memberInfo['school_major'];
                $result['school_education'] = $memberInfo['school_education'];
                $result['company_name'] = $detail['company_name'];
                $result['position'] = $detail['position'];
                $result['start_time'] = $detail['start_time'];
                $result['end_time'] = $detail['end_time'];
                $result['description'] = $detail['description'];
                $result['address'] = $detail['address'];
                $result['salary'] = $detail['salary'];
                $result['create_time'] = $detail['create_time'];
                return self::success($result, 200, 'ok');
            } else {
                $result = [];
                $result['id'] = 0;
                $result['face'] = $memberInfo['face'];
                $result['username'] = $memberInfo['username'];
                $result['school'] = $memberInfo['school'];
                $result['email'] = $memberInfo['email'];
                $result['sex'] = $memberInfo['sex'] == 1 ? "男" : "女";
                $result['mobile'] = $memberInfo['mobile'];
                $result['school_time'] = $memberInfo['school_time'];
                $result['school_major'] = $memberInfo['school_major'];
                $result['school_education'] = $memberInfo['school_education'];
                $result['company_name'] = '';
                $result['position'] = '';
                $result['start_time'] = 0;
                $result['end_time'] = 0;
                $result['description'] = '';
                $result['address'] = '';
                $result['salary'] = '';
                $result['create_time'] = 0;
                return self::success($result, 200, 'ok');
            }
        } else {
            return self::error('请登录', 208);
        }

    }

    /**
     * 编辑更新
     *
     * @param $userId
     * @param $params
     * @return array
     */
    public static function edit($userId, $params)
    {
        if (!$userId) {
            return self::error('请登录', 208);
        }
        if (!$params) {
            return self::error('请求参数有误', 208);

        }
        $id = $params['id'];
        unset($params['id']);
        $params['update_time'] = time();
        $detail = ResumeModel::first(['member_id' => $userId, 'id' => $id]);
        if ($detail) {
            $up = ResumeModel::updated(['id' => $id], $params);
            if ($up) {
                return self::success(['flag' => 1], 200, '更新成功');
            } else {
                return self::error('更新失败', 206);
            }
        } else {
            return self::error('简历不存在', 206);
        }
    }
}